﻿using IDEAMS.core.alarm;
using IDEAMS.core.queue;
using IDEAMS.utils;
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Media;
using System.Text;
using System.Threading.Tasks;

namespace IDEAMS.core.worker
{
    public class AlarmWorker : QueueWorker<QueueData>
    {
        private static ILog logger = LogManager.GetLogger(typeof(AlarmWorker));
        private SHSoundPlayer sp = new SHSoundPlayer();

        private bool alarmSoundOn = false;

        public AlarmWorker()
        {
            alarmSoundOn = TW_App.globalSetting.alarmSoundOn;
            string soundFile = TW_App.fileManager.soundPath + "\\alarm.wav";
            sp.Load(soundFile);
        }
        public override void Start()
        {
            this.stopFlag = false;
            this.startTime = DateTime.Now;

            if (PreStart() == -1)
            {
                logger.ErrorFormat("AlarmWorker启动失败");
                goto end;
            }

            while (!stopFlag)
            {

                QueueData alarmData = null;
                bool rn = recvQueue.TryDequeue(out alarmData, 3000);
                if (!rn)
                {
                    logger.DebugFormat("没有alarm event");
                    continue;
                }

                AlarmEvent alarmEvent = (alarmData as AlarmQueueData).alarmEvent;
                if (alarmEvent != null)
                {
                    if (alarmEvent.soundOn && alarmSoundOn)
                    {
                        //sound on
                        sp.PlayLooping();
                    }


                    if (alarmEvent.uploadOn)
                    {
                        //upload
                    }
                }
            }
        end:
            AfterStart();
        }

        public void AlarmOn()
        {
            alarmSoundOn = true;
            // sp.PlayLooping();
        }

        public void AlarmOff()
        {
            alarmSoundOn = false;
            sp.Stop();
        }
    }
}
